package com.ymt.bpm.service.platform.websocket;

import com.ymt.bpm.util.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
public class WebSocketService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SimpMessagingTemplate template;

    public void sendMessage(String tenantId, String topic, String type, String message) throws Exception{
        if (log.isDebugEnabled()) {
            log.debug("sendMessage tenantId=" + tenantId);
            log.debug("sendMessage topic=" + topic);
            log.debug("sendMessage type=" + type);
            log.debug("sendMessage message=" + message);
        }
        tenantId = tenantId==null ? "0" : tenantId;
        template.convertAndSend("/topic/" + AES.encrypt(tenantId) + '/' + topic, new WsMessage(type, message));
    }
}
